home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue50 / IPC / File Mappings / Delphi / ParentMainFormUnit.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-08-31  |  1.6 KB  |  83 lines

  1. unit ParentMainFormUnit;
  2.  
  3. {$define Mutex}
  4.  
  5. interface
  6.  
  7. uses
  8.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  9.   ExtCtrls, StdCtrls, IPCThrd;
  10.  
  11. type
  12.   TForm1 = class(TForm)
  13.     Memo1: TMemo;
  14.     Timer1: TTimer;
  15.     procedure FormCreate(Sender: TObject);
  16.     procedure FormDestroy(Sender: TObject);
  17.     procedure Timer1Timer(Sender: TObject);
  18.   private
  19.     { Private declarations }
  20.   public
  21.     MemMapFile: TSharedMem;
  22.   {$ifdef Mutex}
  23.     Mutex: TMutex;
  24.   {$endif}
  25.   end;
  26.  
  27.   EWin32Error = class(Exception);
  28.  
  29. var
  30.   Form1: TForm1;
  31.  
  32. const
  33.   MemMapFileName = 'SampleMemoryMappedFile';
  34.   MemMapSize = 1000;
  35.  
  36. implementation
  37.  
  38. {$R *.DFM}
  39.  
  40. function LaunchChildApp: THandle;
  41. const
  42.   ChildApp = 'ChildMemoryMappedFile.Exe';
  43. var
  44.   SI: TStartupInfo;
  45.   PI: TProcessInformation;
  46. begin
  47.   GetStartupInfo(SI);
  48.   if not CreateProcess(nil, ChildApp, nil, nil, False,
  49.      0, nil, nil, SI, PI) then
  50.     raise EWin32Error.Create('Unable to launch ' + ChildApp);
  51.   Result := PI.HProcess
  52. end;
  53.  
  54. procedure TForm1.FormCreate(Sender: TObject);
  55. begin
  56. {$ifdef Mutex}
  57.   Mutex := TMutex.Create('FileMappingMutex');
  58. {$endif}
  59.   MemMapFile := TSharedMem.Create(MemMapFileName, MemMapSize);
  60.   LaunchChildApp;
  61. end;
  62.  
  63. procedure TForm1.FormDestroy(Sender: TObject);
  64. begin
  65.   MemMapFile.Free
  66. end;
  67.  
  68. procedure TForm1.Timer1Timer(Sender: TObject);
  69. begin
  70.   if PChar(MemMapFile.Buffer)[0] <> #0 then
  71.   begin
  72. {$ifdef Mutex}
  73.     Mutex.Get(Integer(Infinite));
  74. {$endif}
  75.     Memo1.Text := StrPas(PChar(MemMapFile.Buffer));
  76. {$ifdef Mutex}
  77.     Mutex.Release
  78. {$endif}
  79.   end
  80. end;
  81.  
  82. end.
  83.